package _022_2_16

import "sort"

type ListNode struct {
	Val  int
	Next *ListNode
}

func sortList(head *ListNode) *ListNode {
	result := make([]*ListNode, 0)

	for head != nil {
		result = append(result, head)
		head = head.Next
	}

	sort.Slice(result, func(i, j int) bool {
		if result[i].Val < result[j].Val {
			return true
		}

		return false
	})

	for i := 0; i < len(result)-1; i++ {
		result[i].Next = result[i+1]
	}

	if len(result) <= 0 {
		return nil
	}

	return result[0]
}
